<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="采购订单编号" prop="displayPoNumber">
            <el-input v-model="formData.displayPoNumber" placeholder="请输入采购订单编号" readonly />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="计划单号" prop="planId">
            <el-input v-model="formData.planId" placeholder="请输入计划单号" readonly />
          </el-form-item>
        </el-col>


      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="formData.projectName" placeholder="请输入项目名称"  readonly/>
          </el-form-item>
        </el-col>
          <el-col :span="12">
            <el-form-item label="计划员" prop="planner">
              <el-input v-model="formData.planner" placeholder="请输入计划员" readonly />
            </el-form-item>

          </el-col>

      </el-row>
      <el-row>


        <el-col :span="12">
          <el-form-item label="物料名称" prop="itemName">
            <el-input v-model="formData.itemName" placeholder="请输入物料名称"  readonly/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="需求数量" prop="demandQuantity">
            <el-input v-model="formData.demandQuantity" placeholder="请输入需求数量" readonly />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
<!--        <el-col :span="8">-->
<!--          <el-form-item label="配置日期" prop="matingDate">-->
<!--            <el-date-picker-->
<!--              v-model="formData.matingDate"-->
<!--              type="date"-->
<!--              value-format="x"-->
<!--              placeholder="选择配置日期"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col :span="12">-->
<!--          <el-form-item label="需求日期" prop="demandDate">-->
<!--            <el-date-picker-->
<!--              v-model="formData.demandDate"-->
<!--              type="date"-->
<!--              value-format="x"-->
<!--              placeholder="选择需求日期"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="12">
          <el-form-item label="需求日期" prop="demandDate">
            <el-input v-model="formData.demandDate" placeholder="选择配置日期" readonly />
          </el-form-item>
        </el-col>

      </el-row>

      <el-divider content-position="left">更新承诺发货日期</el-divider>

      <el-row>
        <el-col :span="12">
          <el-form-item label="承诺日期" prop="promiseMatingDate">
            <el-date-picker
              v-model="formData.promiseMatingDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择承诺配置日期"
            />
          </el-form-item>
        </el-col>
<!--        <el-col :span="8">-->
<!--          <el-form-item label="技术通知号" prop="technicalNotice">-->
<!--            <el-input v-model="formData.technicalNotice" placeholder="请输入技术通知号" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col :span="8">-->
<!--          <el-form-item label="送货状态" prop="shdStatus">-->
<!--            <el-select v-model="formData.shdStatus" placeholder="请选择送货状态">-->
<!--              <el-option-->
<!--                v-for="dict in getStrDictOptions(DICT_TYPE.INFRA_JOB_STATUS)"-->
<!--                :key="dict.value"-->
<!--                :label="dict.label"-->
<!--                :value="dict.value"-->
<!--              />-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->

<!--        <el-col :span="8">-->
<!--          <el-form-item label="发布标识" prop="releaseFlagDesc">-->
<!--            <el-input v-model="formData.releaseFlagDesc" placeholder="请输入发布标识" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
      </el-row>



<!--      <el-form-item label="物料编码" prop="itemCode">-->
<!--        <el-input v-model="formData.itemCode" placeholder="请输入物料编码" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商代码" prop="vendorCode">-->
<!--        <el-input v-model="formData.vendorCode" placeholder="请输入供应商代码" />-->
<!--      </el-form-item>-->


<!--      <el-form-item label="供应商名称" prop="vendorName">-->
<!--        <el-input v-model="formData.vendorName" placeholder="请输入供应商名称" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="项目代码" prop="projectCode">-->
<!--        <el-input v-model="formData.projectCode" placeholder="请输入项目代码" />-->
<!--      </el-form-item>-->
<!--  -->
<!--  -->
<!--      <el-form-item label="列份" prop="columnNumber">-->
<!--        <el-input v-model="formData.columnNumber" placeholder="请输入列份" />-->
<!--      </el-form-item>-->



    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PlanItemsApi, PlanItemsVO } from '@/api/ck/planitems'

/** 送货计划 表单 */
defineOptions({ name: 'PlanItemsForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  matingDate: undefined,
  itemCode: undefined,
  vendorCode: undefined,
  demandDate: undefined,
  promiseMatingDate: undefined,
  vendorName: undefined,
  projectCode: undefined,
  itemName: undefined,
  projectName: undefined,
  technicalNotice: undefined,
  demandQuantity: undefined,
  releaseFlagDesc: undefined,
  columnNumber: undefined,
  planner: undefined,
  planId: undefined,
  displayPoNumber: undefined,
  status: undefined,
  shdStatus: undefined,
})
const formRules = reactive({
  promiseMatingDate: [{ required: true, message: '承诺日期不能为空', trigger: 'change' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PlanItemsApi.getPlanItems(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PlanItemsVO
    if (formType.value === 'create') {
      await PlanItemsApi.createPlanItems(data)
      message.success(t('common.createSuccess'))
    } else {
      await PlanItemsApi.updatePlanItems(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    matingDate: undefined,
    itemCode: undefined,
    vendorCode: undefined,
    demandDate: undefined,
    promiseMatingDate: undefined,
    vendorName: undefined,
    projectCode: undefined,
    itemName: undefined,
    projectName: undefined,
    technicalNotice: undefined,
    demandQuantity: undefined,
    releaseFlagDesc: undefined,
    columnNumber: undefined,
    planner: undefined,
    planId: undefined,
    displayPoNumber: undefined,
    status: undefined,
    shdStatus: undefined,
  }
  formRef.value?.resetFields()
}
</script>
